﻿<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <script src="FileSaver.min.js"></script>
    <!-- or https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.min.js -->
    <script src="sm2.js"></script>
    <script>
        //var armorpubkey, armorseckey;
        var pubkey, seckey;

        function createkeypair() {
            alert("enter createkeypair");
            let keypair = sm2.generateKeyPairHex();
            //alert(keypair);
            pubkey = keypair.publicKey // 公钥
            seckey = keypair.privateKey // 私钥
            //alert(pubkey);
            //alert(seckey);

            // 默认生成公钥 130 位太长，可以压缩公钥到 66 位
            //let compressedpubkey = sm2.compressPublicKeyHex(pubkey) // compressedpubkey 和 pubkey 等价
            //alert(compressedpubkey);
            //if (sm2.comparePublicKeyHex(pubkey, compressedpubkey)) {
            //alert("compare pass");
            //} else {
            //    alert("compare fail")
            //} // 判断公钥是否等价

            // 自定义随机数，参数会直接透传给 jsbn 库的 BigInteger 构造器
            // 注意：开发者使用自定义随机数，需要自行确保传入的随机数符合密码学安全
            //let keypair2 = sm2.generateKeyPairHex('123123123123123')
            //let keypair3 = sm2.generateKeyPairHex(256, SecureRandom)

            //let verifyResult = sm2.verifyPublicKey(pubkey) // 验证公钥
            //alert(verifyResult);
            //verifyResult = sm2.verifyPublicKey(compressedpubkey) // 验证公钥
            //alert(verifyResult);

            //var pub = new Blob([pubkey], { type: "text/plain;charset=utf-8" });
            //alert(typeof (pubkey));
            alert("Save the Publc Key:\n" + pubkey);
            saveAs(pub, "key.pub");

            //var sec = new Blob([seckey], { type: "text/plain;charset=utf-8" });
            alert("Save the Private Key:\n" + seckey);
            saveAs(sec, "key.sec");
        }

        function ReadPubKey() {
            var file = document.getElementById("encryptPubKey").files[0];
            var reader = new FileReader();
            reader.onload = function () {
                pubkey = this.result;
            }

            reader.readAsText(file);
        }

        function ReadSecKey() {
            var file = document.getElementById("encryptSecKey").files[0];
            var reader = new FileReader();
            reader.onload = function () {
                seckey = this.result;
            }

            reader.readAsText(file);
        }

        async function encrypt() {
            var inputtext = document.getElementById("inputtext");
            var plaintext = inputtext.value.toString();
            var plain = document.getElementById("plain");
            plain.textContent = plaintext;

            var cipherMode = 1 // 1 - C1C3C2，0 - C1C2C3，默认为1

            var encrypted = document.getElementById("encrypted");
            var encryptData = sm2.doEncrypt(plaintext, pubkey); // 加密结果
            alert("encryptData" + encryptData);

            encrypted.textContent = encryptData;
        }

        async function decrypt() {
            var plain = document.getElementById("plain");
            var encrypted = document.getElementById("encrypted");

            var encryptData = encrypted.textContent;
            let decryptData = sm2.doDecrypt(encryptData, seckey);
            alert("decryptData" + decryptData);

        }

        async function signit() {
            var inputtext = document.getElementById("inputtext");
            var sig = document.getElementById("sig");

            let msg = inputtext.value;
            let sigValueHex = sm2.doSignature(msg, seckey) // 签名
            //let verifyResult = sm2.doVerifySignature(msg, sigValueHex, publicKey) // 验签结果
            sig.textContent = sigValueHex;
        }

        async function verifyit() {
            var sig = document.getElementById("sig");
            var inputtext = document.getElementById("inputtext");
            var sigValueHex = sig.textContent;
            let msg = inputtext.value;
            let verifyResult = sm2.doVerifySignature(msg, sigValueHex, pubkey) // 验签结果

            alert("meg:\n"+msg+"\nsig:\n"+sigValueHex+"\nresult:"+verifyResult);
        }

    </script>
</head>

<body>
    <hr />
    <input type="button" id="createkey" value="创建密钥" onclick="createkeypair()"></input>
    <br />
    <hr />
    <label>加密 | 读取公钥文件</label><br />
    <input type="file" id="encryptPubKey" value="读取公钥文件" multiple="" onchange="ReadPubKey()" />
    <input type="button" id="encrypt" value="加密" onclick="encrypt()" />
    <input type="button" id="verifyit" value="验证" onclick="verifyit()" /><br />
    <input type="file" id="encryptSecKey" value="读取私钥文件" multiple="" onchange="ReadSecKey()" />
    <input type="button" id="decrypt" value="解密" onclick="decrypt()" />
    <input type="button" id="signit" value="签名" onclick="signit()" />
    <br />
    <hr />
    <textarea cols="40" rows="16" id="inputtext"></textarea>
    <hr />

    <pre>
    <p id="plain"></p>
    <p id="encrypted"></p>
    <p id="sig"></p>
    </pre><br /><br />

</body>

</html>